/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.web.servlet;

import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.utils.CommonUtils;
import han.bifang.web.query.QueryHelper;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import v1.query.web.bifang.han.QueryRequestDocument;
import v1.query.web.bifang.han.QueryResponseDocument;

/**
 * Servlet implementation class for Servlet: QueryServlet
 * 
 */
public class QueryServlet extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public QueryServlet() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		QueryResponseDocument ret = null;
		try {
			String xml = ServletReqResHeler.getRequestString(request);
			QueryRequestDocument doc = QueryRequestDocument.Factory.parse(xml);
			if (!doc.validate()) {
				throw new BifangException(BifangErrorCode.REQUEST_XML_FORMAT_ERROR,
				"request xml format error");
			}
			if(doc.getQueryRequest().getService() != null){
				ret = QueryHelper.queryService(doc.getQueryRequest().getService().getServiceCode());
			}else if(doc.getQueryRequest().getBus() != null){
				if(doc.getQueryRequest().getBus().getBusId() != null)
					ret = QueryHelper.queryServiceBus(doc.getQueryRequest().getBus().getBusId(),null);
				else
					ret = QueryHelper.queryServiceBus(null,doc.getQueryRequest().getBus().getBusCode());
			}else {
				if(CommonUtils.isEmpty(doc.getQueryRequest().getCatalog())){
					ret = QueryHelper.queryDefaultServiceCatalog();
				}
			}
			
		} catch(BifangException e){
			Log logger = LogFactory.getLog(QueryServlet.class);
			logger.error("QueryServlet.doPost()", e);
			ret = QueryResponseDocument.Factory.newInstance();
			ret.addNewQueryResponse().addNewError().setErrorCode(e.getCode());
			ret.getQueryResponse().getError().setErrorMsg(e.getMsg());
		}catch(Exception e){
			Log logger = LogFactory.getLog(QueryServlet.class);
			logger.error("QueryServlet.doPost()", e);
			ret = QueryResponseDocument.Factory.newInstance();
			ret.addNewQueryResponse().addNewError().setErrorCode(BifangErrorCode.UNKNOWN_ERROR);
			ret.getQueryResponse().getError().setErrorMsg("error : " + e.getMessage());
		}
		
		ServletReqResHeler.setResponseString(response, ret.toString());
	}
}